<?php namespace system\model;
use houdunwang\model\Model;
class Links extends Model{
	//数据表
	protected $table = "links";

	//允许填充字段
	protected $allowFill = ['*' ];

	//禁止填充字段
	protected $denyFill = [ ];

	//自动验证
	protected $validate=[
		//['字段名','验证方法','提示信息',验证条件,验证时间]
	];

	//自动完成
	protected $auto=[
		//['字段名','处理方法','方法类型',验证条件,验证时机]
	];

	/**
	 * 获得多少条友链
	 */
	public function getlinks($num){
		return Db::table('links')->orderBy('lsort','DESC')->limit($num)->get();
	}

	/**
	 * 链接添加
	 */
  public  function linkadd(){
	  $this->message($_POST['lname'],'请填写链接名称');
	  $this->message($_POST['logo'],'请上传链接图片');
	  $this->message($_POST['url'],'请填写正确的链接地址');
	  $this->message($_POST['lsort'],'请填写排序权重');
	  $res=Db::table('links')->insert([
		  'lname'=>$_POST['lname'],
		  'logo'=>$_POST['logo'],
		  'url'=>$_POST['url'],
		  'lsort'=>$_POST['lsort'],
		  'addtime'=>time(),
	  ]);
	  if($res){
		  return ['valid'=>1,'message'=>'操作成功'];
	  }else{
		  return ['valid'=>0,'message'=>'操作失败'];
	  }

  }
	//非空验证方法
	private  function message($data,$msg){
		if(empty($data)){
			message($msg,'back','error');
		}
	}
	/**
	 * 链接编辑
	 */
	public function linkedit($lid){
		$this->message($_POST['lname'],'请填写链接名称');
		$this->message($_POST['logo'],'请上传链接图片');
		$this->message($_POST['url'],'请填写正确的链接地址');
		$this->message($_POST['lsort'],'请填写排序权重');
		$res=Db::table('links')->where("lid",$lid)->update([
			'lname'=>$_POST['lname'],
			'logo'=>$_POST['logo'],
			'url'=>$_POST['url'],
			'lsort'=>$_POST['lsort'],
		]);
		if($res){
			return ['valid'=>1,'message'=>'操作成功'];
		}else{
			return ['valid'=>0,'message'=>'操作失败'];
		}
	}


}